package activity

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	"OnlineRetailers/models/Admin/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type SearchActivity struct {
	beego.Controller
}

func (c *SearchActivity) SearchActivity() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/activity/SearchActivity"
	res := resMes.GetActivity{}
	activity := []*Admin.Activity{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	key := c.GetString("key")
	if key == "" {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "401", "keyNull")
		return
	}
	keyClass := c.GetString("keyClass")
	if keyClass == "" {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "401", "keyClassNull")
		return
	}
	//fmt.Println(begin)
	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 4)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	cond := orm.NewCondition()
	cond1 := cond.Or(keyClass+"__icontains", key)
	_, err = o.QueryTable("activity").SetCond(cond1).All(&activity)
	//fmt.Println(key)
	//sql := "select * from `activity` where `" + keyClass + "` LIKE '%" + key + "%'"
	//_, err = o.Raw(sql).QueryRows(&activity)

	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	res.Activity = activity
	res.Code = "200"
	c.Data["json"] = res

}
